package org.example.exp2.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.exp2.common.CommonResponse;
import org.example.exp2.common.ResponseCode;
import org.example.exp2.entity.BorrowRecord;
import org.example.exp2.entity.BorrowRecordVO;
import org.example.exp2.entity.Person;
import org.example.exp2.mapper.BorrowRecordMapper;
import org.example.exp2.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author junchao
 * @version V1.0
 * Copyright (c) 2024, zhoujunchao@csu.edu.com All Rights Reserved.
 * @ProjectName:exp2_mybatis_plus
 * @Title: BorrowRecordController
 * @Package org.example.exp2.controller
 * @Description:
 * @date 2024/11/25 8:43
 */

@RestController
@RequestMapping("/records")
@Tag(name = "图书管理API")
public class BorrowRecordController {
//    @Autowired
//    BorrowRecordService borrowRecordService;

    @Autowired
    BorrowRecordMapper borrowRecordMapper;

    @Value("${server.port}")
    private int serverPort;

    public String getUrl() {
        return "http://localhost:" + serverPort;
    }

    @GetMapping("")
    @Operation(summary = "获取借阅记录", description = "获取所有借阅记录")
    public CommonResponse<Map<String, Object>> getAllRecords() {
        List<BorrowRecord> records = borrowRecordMapper.selectList(null);
        List<BorrowRecordVO> borrowRecordVO = records.stream()
                .map(record ->{
                    BorrowRecordVO resource = new BorrowRecordVO(
        record.getRecordId(),
        record.getBookId(),
        record.getPersonId(),
        record.getBorrowDate(),
        record.getReturnDate()
                    );
                    resource.setAllLinks(getUrl());
                    return  resource;
                }).collect(Collectors.toList());
        Map<String, Object> res = new HashMap<>();
        res.put("records", borrowRecordVO);
        res.put("total", records.size());

        CommonResponse<Map<String, Object>> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(), "成功获取全部借阅记录", res);
        commonResponse.getData().addLink("self", getUrl() + "/records");
        return commonResponse;
    }

    @PostMapping("")
    @Operation(summary = "添加借阅记录", description = "添加新借阅记录")
    public CommonResponse<Void> addRecord(
            @Parameter(description = "借阅记录对象",required = true) @RequestBody BorrowRecord record) {
        int result = borrowRecordMapper.insert(record);
        if(result >0) {
            CommonResponse<Void> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(), "新借阅记录添加成功！");
            commonResponse.getData().addLink("self", getUrl() + "/records/" + record.getRecordId());
            return commonResponse;
        }else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"新借阅记录添加失败:(");
        }
    }

    @PutMapping("/{recordId}")
    @Operation(summary = "更新借阅记录", description = "更新recordId对应的还书时间")
    public CommonResponse<Void> returnBook(
            @PathVariable("recordId") int recordId,
            @RequestBody Map<String, Object> updateData) {
        String returnDate = (String) updateData.get("returnDate");
        BorrowRecord record = borrowRecordMapper.selectById(recordId);
        if (record == null) {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(), "借阅记录未找到");
        }
        record.setReturnDate(returnDate);
        int result = borrowRecordMapper.updateById(record);
        if (result > 0) {
            CommonResponse<Void> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(),"借阅记录更新成功！");
            commonResponse.getData().addLink("self",  getUrl() + "/records/" + record.getRecordId());
            return commonResponse;
        } else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"还书时间更新失败:(");
        }
    }

    @DeleteMapping("/{recordId}")
    @Operation(summary = "删除记录",description = "根据recordId从系统中删除借阅记录")
    public CommonResponse<Void> deleteRecord(
            @Parameter(description = "recordId",required = true) @PathVariable("recordId") int recordId){
        int result = borrowRecordMapper.deleteById(recordId);
        if(result>0){
            CommonResponse<Void> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(),"借用记录删除成功!");
            commonResponse.getData().addLink("records",getUrl()+"/records");
            return commonResponse;
        }else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"借用记录删除失败:(");
        }
    }
}